$("#adult").change(function()
{ 
	var adult = $(this).val();
	var infant = $("#infant").val();

	if(infant > adult)
	{
		$('#infant').val(adult);
		$('#uniform-infant > span').text(adult);
	}
});

$("#infant").change(function()
{ 
	var adult = $("#adult").val();
	var infant = $(this).val();

	if(infant > adult)
	{
		$(this).val(adult);
		$('#uniform-infant > span').text(adult);
		$("#error_infant").fadeIn();
		$("#error_infant").fadeIn("slow");
		$("#error_infant").fadeIn(3000);
		$("#error_infant").delay( 3000 ).fadeOut();
		$("#error_infant").fadeOut("slow");
		$("#error_infant").fadeOut(3000);
		$("#error_infant span").html('Jumlah bayi salah');
	}
});

function datepicker_flight(id_depdate, id_retdate)
{
	$('#'+id_depdate).datepicker({
		monthNames: [ "Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember" ],
		dayNames: [ "Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu" ],
		dayNamesMin: [ "M", "S", "S", "R", "K", "J", "S" ],
		showOn: 'both',
		dateFormat: 'yy-mm-dd',
		inline: true,
		minDate: +1,
		maxDate: "+12M",
		buttonImage: '/images/ico/calendar.png',
		buttonImageOnly: true,
		defaultDate : +1,
		onSelect: function() {
			if ($('#'+id_depdate).val() > $('#'+id_retdate).val())
			{
				var b = window.navigator.userAgent ;
				if((b.indexOf('Safari') != -1))
				{
					var depdate_val = $('#'+id_depdate).val().replace('-','/');
					depdate_val = depdate_val.replace('-','/');
					var ndate = new Date(depdate_val);
				}
				else
				{
					var ndate = new Date($('#'+id_depdate).val());
				}
				ndate.setDate(ndate.getDate());
				var year = ndate.getFullYear();
				var month = ndate.getMonth() + 1;
				var day = ndate.getDate();

				year = year.toString();
				month = month.toString();
				day = day.toString();
				if(month.length == 1)
				{
					month = '0' + month; 
				}
				if(day.length == 1)
				{
					day = '0' + day; 
				}
				$('#'+id_retdate).val(year+'-'+month+'-'+day);
			}
                        if(document.getElementById('to_input').value == '' && document.getElementById('to').value == '')
                        {
                            $("#dialog_ow").fadeIn();
                            $("#dialog_ow").fadeIn("fast");
                        }
                        else if(document.getElementById('from_input').value == '' && document.getElementById('from').value == '')
                        {
                            $("#dialog_ow").fadeIn();
                            $("#dialog_ow").fadeIn("fast");
                        }
                        else if(document.getElementById('pulangpergi').checked == true && document.getElementById('retdate').value == '')
                        {
                            $("#retdate").focus();
                        }
		}
	});
        
	$('#'+id_retdate).datepicker({
		monthNames: [ "Januari", "Februari", "Maret", "April", "Mei", "Juni", "Juli", "Agustus", "September", "Oktober", "November", "Desember" ],
		dayNames: [ "Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jumat", "Sabtu" ],
		dayNamesMin: [ "M", "S", "S", "R", "K", "J", "S" ],
		showOn: 'both',
                dateFormat: 'yy-mm-dd',
                inline: true,
                minDate: +1,
		maxDate: "+12M",
		buttonImage: '/images/ico/calendar.png',
		buttonImageOnly: true,
		beforeShow: function (input, inst) 
		{
			var b = window.navigator.userAgent ;
			if((b.indexOf('Safari') != -1))
			{
				var get_date = $('#'+id_depdate).val();
				get_date = get_date.replace('-','/');
				get_date = get_date.replace('-','/');

				var ndate = new Date(get_date);
			}
			else
			{
				var ndate = new Date($('#'+id_depdate).val());
			}
			ndate.setDate(ndate.getDate());
			var year = ndate.getFullYear();
			var month = ndate.getMonth() + 1;
			var day = ndate.getDate();
			year = year.toString();
			month = month.toString();
			day = day.toString();
			if(month.length == 1)
			{
				month = '0' + month; 
			}
			if(day.length == 1)
			{
				day = '0' + day; 
			}
			inst.settings.minDate =year+'-'+month+'-'+day;
		}
	});
}

//populate list city for autocomplete
//id_input is what customer see and for autocomplete purposes
//id_input_post is what system post
function populate_city_flight(id_input, id_input_post, data)
{
	//parameter id = send input id
	$('#'+id_input).autocomplete({
		source: data,select: function(event, ui)
		{
			$('#'+id_input_post).val(ui.item.city_code);
		}
	});
}

function form_flight_main()
{
var valid = '1';

    if($('#from').val() == '')
    {
        $("#error_from").fadeIn();
        $("#error_from").fadeIn("slow");
        $("#error_from").fadeIn(3000);
        $("#error_from").delay( 3000 ).fadeOut();
        $("#error_from").fadeOut("slow");
        $("#error_from").fadeOut(3000);
        $("#error_from span").html('Isi kota berangkat');
        valid = '0';
    }
    
    if($('#to').val() == '')
    {
        $("#error_to").fadeIn();
        $("#error_to").fadeIn("slow");
        $("#error_to").fadeIn(3000);
        $("#error_to").delay( 3000 ).fadeOut();
        $("#error_to").fadeOut("slow");
        $("#error_to").fadeOut(3000);
        $("#error_to span").html('Isi kota berangkat');
        valid = '0';
    }
    
    if($('#from_input').val() == '')
    {
        $("#error_from").fadeIn();
        $("#error_from").fadeIn("slow");
        $("#error_from").fadeIn(3000);
        $("#error_from").delay( 3000 ).fadeOut();
        $("#error_from").fadeOut("slow");
        $("#error_from").fadeOut(3000);
        $("#error_from span").html('Isi kota berangkat');
        valid = '0';
    }
    
    if($('#to_input').val() == '')
    {
        $("#error_to").fadeIn();
        $("#error_to").fadeIn("slow");
        $("#error_to").fadeIn(3000);
        $("#error_to").delay( 3000 ).fadeOut();
        $("#error_to").fadeOut("slow");
        $("#error_to").fadeOut(3000);
        $("#error_to span").html('Isi kota tujuan');
        valid = '0';
    }
    
    if($('#to_input').val() == $('#from_input').val() && $('#to_input').val() != '')
    {
        $("#error_to").fadeIn();
        $("#error_to").fadeIn("slow");
        $("#error_to").fadeIn(3000);
        $("#error_to").delay( 3000 ).fadeOut();
        $("#error_to").fadeOut("slow");
        $("#error_to").fadeOut(3000);
        $("#error_to span").html('Kota berangkat dan tujuan tidak boleh sama');
        valid = '0';
    }
    
    if($('#to').val() == $('#from').val() && $('#to').val() != '')
    {
        $("#error_to").fadeIn();
        $("#error_to").fadeIn("slow");
        $("#error_to").fadeIn(3000);
        $("#error_to").delay( 3000 ).fadeOut();
        $("#error_to").fadeOut("slow");
        $("#error_to").fadeOut(3000);
        $("#error_to span").html('Kota berangkat dan tujuan tidak boleh sama');
        valid = '0';
    }
    
    if($('#depdate').val() == '')
    {
        $("#error_depdate").fadeIn();
        $("#error_depdate").fadeIn("slow");
        $("#error_depdate").fadeIn(3000);
        $("#error_depdate").delay( 3000 ).fadeOut();
        $("#error_depdate").fadeOut("slow");
        $("#error_depdate").fadeOut(3000);
        $("#error_depdate span").html('Isi tanggal berangkat');
        valid = '0';
    }
    
    if($('#pulangpergi').is(':checked'))
    {
        if($('#retdate').val() == '')
        {
            $("#error_retdate").fadeIn();
            $("#error_retdate").fadeIn("slow");
            $("#error_retdate").fadeIn(3000);
            $("#error_retdate").delay( 3000 ).fadeOut();
            $("#error_retdate").fadeOut("slow");
            $("#error_retdate").fadeOut(3000);
            $("#error_retdate span").html('Isi tanggal kembali');
            valid = '0';
        }
    }
    if(valid == '0')
    {
        return false;
    }
    else if(valid == '1')
    {
        return true;
    } 
}

if(window.innerWidth >= 1024)
{
    $( "#from_input" ).focus(function() {

        $("#dialog_rt").fadeOut();
        $("#dialog_rt").fadeOut("fast");
        $("#dialog_ow").fadeIn();
        $("#dialog_ow").fadeIn("fast");
        $('html, body').animate({
                scrollTop: ($('#tiket').offset().top)
        },500);
//        var getclass = document.getElementById('switch_LO_rt').getAttribute('class');
//        if(getclass == 'choose')
//        {
//            switch_type('LO','ow')
//        }
//        else
//        {
//            switch_type('IN','ow')
//        }

    });
    $("#from_input").keypress(function(){
        $("#dialog_ow").fadeOut();
        $("#dialog_ow").fadeOut("fast");
    });
    $( "#to_input" ).focus(function() {
        $("#dialog_ow").fadeOut();
        $("#dialog_ow").fadeOut("fast");
        $("#dialog_rt").fadeIn();
        $("#dialog_rt").fadeIn("fast");
        $('html, body').animate({
            scrollTop: ($('#tiket').offset().top)
        },500);
//        var getclass = document.getElementById('switch_LO_ow').getAttribute('class');
//        if(getclass == 'choose')
//        {
//            switch_type('LO','rt')
//        }
//        else
//        {
//            switch_type('IN','rt')
//        }
    });

    $("#to_input").keypress(function(){
        $("#dialog_rt").fadeOut();
        $("#dialog_rt").fadeOut("fast");
    });
    
    $('body').click(function(event) {
        var target = $(event.target);
        if (target.parents('#dialog_ow').length == 0 && target.parents('#dialog_rt').length == 0 && event.target.id != 'from_input')
        {
            $('#dialog_ow').fadeOut();
        }
        if (target.parents('#dialog_ow').length == 0 && target.parents('#dialog_rt').length == 0 && event.target.id != 'to_input')
        {
            $('#dialog_rt').fadeOut();
        }
    });
}
function switch_type(word,type)
{
    if(word == 'IN')
    {
        var switch_IN = document.getElementById('switch_IN_'+type);
        var switch_LO = document.getElementById('switch_LO_'+type);
        switch_LO.setAttribute('class', '');
        switch_IN.setAttribute('class', 'choose');
        
        if(type == 'rt')
        {
            var get_area = document.getElementById('city_contain_rt');
        }
        else if(type == 'ow')
        {
            var get_area = document.getElementById('city_contain_ow');
        }
        var get_class_LO = get_area.getElementsByClassName('LO');
        var get_class_IN = get_area.getElementsByClassName('IN');

        for(var i = 0; i<get_class_IN.length; i++)
        {
            get_class_IN[i].style.display = 'block';
        }
        for(var i = 0; i<get_class_LO.length; i++)
        {
            get_class_LO[i].style.display = 'none';
        }
    }
    else if(word == 'LO')
    {
        var switch_LO = document.getElementById('switch_LO_'+type);
        var switch_IN = document.getElementById('switch_IN_'+type);
        switch_IN.setAttribute('class', '');
        switch_LO.setAttribute('class', 'choose');
        
        if(type == 'rt')
        {
            var get_area = document.getElementById('city_contain_rt');
        }
        else if(type == 'ow')
        {
            var get_area = document.getElementById('city_contain_ow');
        }
        var get_class_LO = get_area.getElementsByClassName('LO');
        var get_class_IN = get_area.getElementsByClassName('IN');
        for(var i = 0; i<get_class_IN.length; i++)
        {
            get_class_IN[i].style.display = 'none';
        }
        for(var i = 0; i<get_class_LO.length; i++)
        {
            get_class_LO[i].style.display = 'block';
        }
    }
}

function set_city(city_name,city_slug, type)
{
    if(type == 'ow')
    {
        document.getElementById('from_input').value = city_name;
        document.getElementById('from').value = city_slug;
        $("#dialog_ow").fadeOut();
        $("#dialog_ow").fadeOut("fast");
        
        if(document.getElementById('to_input').value == '' && document.getElementById('to').value == '')
        {
            $("#dialog_rt").fadeIn();
            $("#dialog_rt").fadeIn("fast");
        }
        else if(document.getElementById('depdate').value == '')
        {
            $("#depdate").focus();
        }
        else if(document.getElementById('pulangpergi').checked == true && document.getElementById('retdate').value == '')
        {
            $("#retdate").focus();
        }
    }
    else if(type == 'rt')
    {
        document.getElementById('to_input').value = city_name;
        document.getElementById('to').value = city_slug;
        $("#dialog_rt").fadeOut();
        $("#dialog_rt").fadeOut("fast");
        if(document.getElementById('from_input').value == '' && document.getElementById('from').value == '')
        {
            $("#dialog_ow").fadeIn();
            $("#dialog_ow").fadeIn("fast");
        }
        else if(document.getElementById('depdate').value == '')
        {
            $("#depdate").focus();
        }
        else if(document.getElementById('pulangpergi').checked == true && document.getElementById('retdate').value == '')
        {
            $("#retdate").focus();
        }
    }
}